<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="咨询开始时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeStartTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="咨询结束时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeEndTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="咨询类型" prop="counselorType">
        <el-select v-model="queryParams.counselorType" placeholder="请选择咨询类型" clearable>
          <el-option
            v-for="dict in consultation_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="风险评估" prop="riskAssessment">
        <el-select v-model="queryParams.riskAssessment" placeholder="请选择风险评估" clearable>
          <el-option
            v-for="dict in risk_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="resultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="咨询结果表" align="center" prop="resultId" />
      <el-table-column label="咨询开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="咨询结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="咨询类型" align="center" prop="counselorType">
        <template #default="scope">
          <dict-tag :options="consultation_type" :value="scope.row.counselorType"/>
        </template>
      </el-table-column>
      <el-table-column label="进展" align="center" prop="progressEvaluation" />
      <el-table-column label="风险评估" align="center" prop="riskAssessment">
        <template #default="scope">
          <dict-tag :options="risk_level" :value="scope.row.riskAssessment"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改咨询结果对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="resultRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主诉问题" prop="mainProblem">
          <el-input v-model="form.mainProblem" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="观察记录" prop="observation">
          <el-input v-model="form.observation" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="评估结果" prop="assessmentResult">
          <el-input v-model="form.assessmentResult" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="干预技术" prop="counselorMethod">
          <el-select v-model="form.counselorMethod" placeholder="请选择干预技术">
            <el-option
              v-for="dict in consult_techniques"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="其他疗法" prop="otherMethod">
          <el-input v-model="form.otherMethod" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="进展" prop="progressEvaluation">
          <el-input v-model="form.progressEvaluation" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="风险评估" prop="riskAssessment">
          <el-select v-model="form.riskAssessment" placeholder="请选择风险评估">
            <el-option
              v-for="dict in risk_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="本次咨询用户满意度" prop="userSatisfaction">
          <el-input v-model="form.userSatisfaction" placeholder="请输入本次咨询用户满意度" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Result">
import { listResult, getResult, delResult, addResult, updateResult } from "@/api/result/result";
import {parseTime} from "@/utils/ruoyi.js";

const { proxy } = getCurrentInstance();
const { consultation_type, consult_techniques, risk_level } = proxy.useDict('consultation_type', 'consult_techniques', 'risk_level');

const resultList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeStartTime = ref([]);
const daterangeEndTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    startTime: null,
    endTime: null,
    counselorType: null,
    riskAssessment: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询咨询结果列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeStartTime && '' != daterangeStartTime) {
    queryParams.value.params["beginStartTime"] = daterangeStartTime.value[0];
    queryParams.value.params["endStartTime"] = daterangeStartTime.value[1];
  }
  if (null != daterangeEndTime && '' != daterangeEndTime) {
    queryParams.value.params["beginEndTime"] = daterangeEndTime.value[0];
    queryParams.value.params["endEndTime"] = daterangeEndTime.value[1];
  }
  listResult(queryParams.value).then(response => {
    resultList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    resultId: null,
    userId: null,
    counselorId: null,
    startTime: null,
    endTime: null,
    counselorType: null,
    mainProblem: null,
    observation: null,
    assessmentResult: null,
    counselorMethod: null,
    otherMethod: null,
    progressEvaluation: null,
    riskAssessment: null,
    userSatisfaction: null
  };
  proxy.resetForm("resultRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeStartTime.value = [];
  daterangeEndTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.resultId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加咨询结果";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _resultId = row.resultId || ids.value
  getResult(_resultId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改咨询结果";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["resultRef"].validate(valid => {
    if (valid) {
      if (form.value.resultId != null) {
        updateResult(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addResult(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _resultIds = row.resultId || ids.value;
  proxy.$modal.confirm('是否确认删除咨询结果编号为"' + _resultIds + '"的数据项？').then(function() {
    return delResult(_resultIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('result/result/export', {
    ...queryParams.value
  }, `result_${new Date().getTime()}.xlsx`)
}

getList();
</script>
